package com.changgou.goods.controller;
import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.pojo.ParentCategory;
import com.changgou.goods.service.CategoryService;
import com.changgou.goods.pojo.Category;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@CrossOrigin
@RequestMapping("/category")
public class CategoryController {


    @Autowired
    private CategoryService categoryService;

    /**
     * 商品列表分类分页查询
     * @param current 当前页
     * @param pageSize 每页记录数
     * @return
     */
    @GetMapping("/{current}/{pageSize}/{category_id}")
    public PageResult<Category> findCategory(@PathVariable int current,@PathVariable int pageSize,@PathVariable Integer category_id){
        if(category_id==null){
            category_id=0;
        }
        Page<Category> categoryList=categoryService.findCategory(current,pageSize,category_id);
        return new PageResult<>(categoryList.getTotal(),categoryList.getResult());
    }

    /**
     * 新增下级分类
     * @param category
     * @return
     */
    @PostMapping("/addLower")
    public Result addLower(@RequestBody Category category){
        categoryService.insertCategory(category);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable int id){
        categoryService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
     * 上级分类下拉选
     * @return
     */
    @GetMapping("/findAllCategory")
    public Result findAllCategory(){
        List<ParentCategory> allCategory = categoryService.findAllCategory();
        return new Result(true,StatusCode.OK,"查询成功",allCategory);

    }

    /**
     * 添加分类
     * @param category
     * @return
     */
    @PostMapping("/addCatgory")
    public Result addCatgory(@RequestBody Category category){
        categoryService.addCatgory(category);
        return new Result(true,StatusCode.OK,"添加成功");

    }

    /**
     * 三级分类查询
     * @return
     */
    @GetMapping("/findsCategory")
    public Result findsCategory(){
        List<ParentCategory> allCategory = categoryService.findsCategory();
        return new Result(true,StatusCode.OK,"查询成功",allCategory);

    }


    /**
     * 选择商品分类 第一步插入一条数据
     * @return
     */
    @PostMapping("/addCategory")
    public Result addCategory(@RequestBody Category category){
        categoryService.addCategory(category);
        return new Result(true,StatusCode.OK,"添加成功",null);

    }

    /**
     * 这个接口跟上面的接口结合使用才能创建一个真正的商品
     * @param id
     * @return
     */
    @PostMapping("/selectOneCategory/{id}")
    public Result selectOneCategory(@PathVariable int id){
        categoryService.selectOneCategory(id);
        return new Result(true,StatusCode.OK,"添加成功",null);

    }

    /**
     * 前台首页导航栏查询
     * @return
     */
    @GetMapping
    public Result findNavigator(){
        List<Category> categoryList = categoryService.findNavigator();
        return new Result(true,StatusCode.OK,"查询成功",categoryList);
    }


}
